package com.MSofLectures.Tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.MSofLectures.DatabaseBasic;
import com.MSofLectures.DAO.User_registerLecture;
import com.MSofLectures.Entity.User.UserImp;
import com.MSofLectures.Operator.Lecture.OperatorOnLecture;
import com.MSofLectures.Operator.Lecture.OperatorOnLectureImp;
import com.MSofLectures.Operator.User.OperatorOnUser;
import com.MSofLectures.Operator.User.OperatorOnUserImp;

public class StudentsInfoCollector {
	/*
	 * @author ufo
	 * 学生信息收集
	 */
	protected OperatorOnUserImp operatorOnUser=null;
	protected OperatorOnLectureImp operatorOnLecture=null;
	private DatabaseControler databaseControler = new DatabaseControler(DatabaseBasic.getSf());

	public List<Map<String, String>> collectStudentsInfo(
			String enterYear, String lecYear) {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		List<String> studentsIdList = getStudents(enterYear);
		List<String> lecturesIdList = getLectures(lecYear);
		for (String tmpStudentId : studentsIdList) {
			Map<String, String> student = new HashMap<String, String>();
			UserImp studentEntity=this.getOperatorOnUser().loadUid(tmpStudentId);
			student.put("userId", tmpStudentId);
			student.put("userName", studentEntity.getValue("userName"));
			student.put("enterYear", studentEntity.getValue("enterYear"));
			student.put("major", studentEntity.getValue("major"));
			student.put("userclass", studentEntity.getValue("userclass"));
			Integer countLectures = 0;// 报名数量
			Integer countCheckedLectures = 0;// 摇号成功数量
			Integer countRegistedLectures = 0;// 签到数量
			for (String tmpLectureId : lecturesIdList) {
				List<?> user_registerLectureList = databaseControler.hibernateSess
						.createQuery(
								"from User_registerLecture where lectureId=:lectureId and userId=:userId")
						.setString("lectureId", tmpLectureId)
						.setString("userId", tmpStudentId).list();
				Iterator<?> tmpUser_registerLecture = user_registerLectureList
						.iterator();
				while (tmpUser_registerLecture.hasNext()) {
					com.MSofLectures.DAO.User_registerLecture user_registerLecture = (User_registerLecture) tmpUser_registerLecture
							.next();
					countLectures++;
					if (1 == user_registerLecture
							.getUser_registerLecture_checked())
						countCheckedLectures++;
					if (null != user_registerLecture
							.getLecture_registerCodeId())
						countRegistedLectures++;
				}
			}
			student.put("countLectures", countLectures.toString());
			student.put("countCheckedLectures", countCheckedLectures.toString());
			student.put("countRegistedLectures",
					countRegistedLectures.toString());
			result.add(student);
		}
		try {
			this.finalize();
		} catch (Throwable e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return result;
	}

	public List<Map<String, String>> collect() {
		List<Map<String, String>> result = new ArrayList<Map<String, String>>();
		return result;
	}

	public List<String> getStudents(String enterYear) {
		List list = databaseControler.hibernateSess
				.createQuery(
						"select userId from User_keyValue where user_keyValue=:user_keyValue and user_keyNameId=:user_keyNameId order by userId")
				.setString("user_keyValue", enterYear)
				.setString("user_keyNameId", getUser_keyNameId("enterYear"))
				.list();
		return list;
	}

	public List<String> getLectures(String lecYear) {
		List<?> list = 9>lecYear.length()?
				databaseControler.hibernateSess
				.createQuery("select lectureId from Lecture")
				.list()
				:databaseControler.hibernateSess
				.createQuery(
						"select lectureId from Lecture_keyValue where lecture_keyValue=:lecYear and lecture_keyNameId=:lecture_keyNameId")
				.setString("lecYear", lecYear)
				.setString("lecture_keyNameId", getLecture_keyNameId("lecYear"))
				.list();
		List<String> result = new ArrayList<String>();
		Iterator<?> i = list.iterator();
		while (i.hasNext()) {
			com.MSofLectures.Entity.Lecture.LectureImp lectureTmp = this.getOperatorOnLecture().loadLecture(i
					.next().toString());
			if(null!=lectureTmp)
					result.add(lectureTmp.getLectureId());
		}
		return result;
	}

	private String getLecture_keyNameId(String lecture_keyName) {
		List<?> list = databaseControler.hibernateSess
				.createQuery(
						"select lecture_keyNameId from Lecture_keyName where lecture_keyName=:lecture_keyName")
				.setString("lecture_keyName", lecture_keyName).list();
		Iterator<?> i = list.iterator();
		if (i.hasNext())
			return i.next().toString();
		else
			return null;
	}

	private String getUser_keyNameId(String user_keyName) {
		List<?> list = databaseControler.hibernateSess
				.createQuery(
						"select user_keyNameId from User_keyName where user_keyName=:user_keyName")
				.setString("user_keyName", user_keyName).list();
		Iterator<?> i = list.iterator();
		if (i.hasNext())
			return i.next().toString();
		else
			return null;
	}
	public void finalize() throws Throwable{//为garbage collection准备，无需关注
		System.out.println("StudentInfoCollector collect");
		if(null!=this.operatorOnLecture)
			this.operatorOnLecture.finalize();
		if(null!=this.operatorOnUser)
			this.operatorOnUser.finalize();
		super.finalize();
	}

	public OperatorOnUserImp getOperatorOnUser() {
		if(null==this.operatorOnUser)
			this.operatorOnUser=new OperatorOnUser();
		return this.operatorOnUser;
	}

	public void setOperatorOnUser(OperatorOnUserImp operatorOnUser) {
		this.operatorOnUser = operatorOnUser;
	}

	public OperatorOnLectureImp getOperatorOnLecture() {
		if(null==this.operatorOnLecture)
			this.operatorOnLecture=new OperatorOnLecture();
		return this.operatorOnLecture;
	}

	public void setOperatorOnLecture(OperatorOnLectureImp operatorOnLecture) {
		this.operatorOnLecture = operatorOnLecture;
	}
}
